package com.gq.utils;

import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.gq.doremon.R;
import com.rtst.widget.tab.SwipeyTabs;
import com.rtst.widget.tab.SwipeyTabsAdapter;

public class ImageViewPager extends RelativeLayout implements OnClickListener {
	private ImageViewPager instance;
	private ImageViewTouch imageView;
	private Button buttonZoomIn;
	private Button buttonZoomOut;
	private Button buttonDownload;
	private ProgressBar progressBar;
	private String url;
	private ImageButton btnNext, btnPrev;
	private CustomViewPager mViewPager;
	private SwipeyTabs msTabs;
	
	public ImageViewPager(Context context) {
		super(context);
	}

	public void init(String url, CustomViewPager viewPager, SwipeyTabs sTabs) {
		setInstance(this);
		msTabs = sTabs;
		mViewPager = viewPager;
		loadAllItem();
		this.url = url;
		getImageView().setmTabs(sTabs);
		getImageView().setImageViewPager(getInstance());
		getImageView().setViewPager(viewPager);
		getImageView().initData();
		getImageView().prepareDownload(url);
	}
	
	public void redownload() {
		getImageView().prepareDownload(url);
	}
	
	public void loadAllItem() {
		imageView = (ImageViewTouch) findViewById(R.id.image_view);
		buttonZoomIn = (Button) findViewById(R.id.button_zoom_in);
		buttonZoomOut = (Button) findViewById(R.id.button_zoom_out);
		progressBar = (ProgressBar) findViewById(R.id.progress_bar);
		btnNext = (ImageButton) findViewById(R.id.btnNext);
		btnPrev = (ImageButton) findViewById(R.id.btnPrevious);
		btnNext.setOnClickListener(this);
		btnPrev.setOnClickListener(this);
	}
	
	public ImageViewPager getInstance() {
		return instance;
	}

	public void setInstance(ImageViewPager instance) {
		this.instance = instance;
	}

	public ImageViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public ImageButton getButtonGoNext() {
		return btnNext;
	}
	
	public ImageButton getButtonGoPrev() {
		return btnPrev;
	}

	public Button getButtonZoomIn() {
		return buttonZoomIn;
	}

	public Button getButtonZoomOut() {
		return buttonZoomOut;
	}

	public Button getButtonDownload() {
		return buttonDownload;
	}

	public ProgressBar getProgressBar() {
		return progressBar;
	}

	public ImageViewTouch getImageView() {
		return imageView;
	}
	
	public void dispose() {
		getImageView().dispose();
	}

	@Override
	public void onClick(View v) {
		int currentPos = mViewPager.getCurrentItem();
		if (v.getId() == btnNext.getId()) {
			int max = ((SwipeyTabsAdapter) mViewPager.getAdapter()).getCount();
			if (currentPos < max - 1) {
				getImageView().zoomTo(1f, 50);
				mViewPager.setCurrentItem(currentPos+1, true);
			}
			if (currentPos == max - 1) {
				Toast.makeText(getContext(), "Trang cuối rồi bạn à !!!", Toast.LENGTH_SHORT).show();
			}
		} else if (v.getId() == btnPrev.getId()) {
			if (currentPos>0) {
				getImageView().zoomTo(1f, 50);
				mViewPager.setCurrentItem(currentPos-1, true);
			}
		}
	}
}
